import {config} from '../utils/config.js';
import { Base } from '../utils/util.js';

class Address extends Base{
  constructor(){
    super();
  }

  setAddressInfo(res){
    var province = res.provinceName || res.province,
      city = res.cityName || res.city,
      country = res.countyName || res.country,
      detail = res.detailInfo || res.detail;
    var totalDetail = city + country + detail;
    if(!this.isCenterCity(province)){
      totalDetail = province + totalDetail;
    }
    return totalDetail;
  }

  /**是否为直辖市 */
  isCenterCity(name){
    var centerCitys = ['北京市', '天津市', '上海市', '重庆市'];
    if (centerCitys.indexOf(name) >= 0){
        return true
    }
  }

  /**更新保存地址 */
  submitAddress(data,callBack){
    data = this._setUpAddress(data);
    this.request({
      url: 'address',
      method:'POST',
      data:data,
      success: function (res) {
        callBack && callBack(res);
      }
    })
  }
  
  /**更新地址所需要的数据*/
  _setUpAddress(res){
    var formData = {
      name: res.userName,
      province: res.provinceName,
      city: res.cityName,
      country: res.countyName,
      mobile: res.telNumber,
      detail: res.detailInfo
    };
    return formData;
  }

  /**获取自己的收货地址 */
  getAddress(callBack){
      var that = this;
      that.request({
        url:'address/getaddress',
        success:function(res){
          console.log('getAddress:',res);
          if(res){
            res.totalDetail = that.setAddressInfo(res);
            callBack && callBack(res);
          }
        }
      })
  }



}
export {Address}






